//
//  LWVisitorView.swift
//  LW_Weibo
//
//  Created by 刘伟 on 16/7/9.
//  Copyright © 2016年 LWSpro. All rights reserved.
//


import UIKit
import SnapKit

//导入第三方框架

class LWVisitorView: UIView {
    
    //定义闭包 目的是为了实现登录按钮和注册按钮的点击事件
    var registerLoginClosure: (() -> ())?
    
    //MARK: - 懒加载
    //旋转视图
    private lazy var cycleImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    //罩层
    private lazy var maskIconImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    //icon
    private lazy var iconImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    private lazy var registerButton: UIButton = {
        
        let button = UIButton()
        
        button.addTarget(self, action: "registerAction", forControlEvents: .TouchUpInside)
        
        
        //  设置字体大小
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
        
        //  设置文本及文本颜色
        button.setTitle("注册", forState: .Normal)
        //字体颜色
        button.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        
        button.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
        //设置背景图片
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        
        
        return button
        
        
    }()
    
    private lazy var loginButton: UIButton = {
        
        let button = UIButton()
        
        button.addTarget(self, action: "loginAction", forControlEvents: .TouchUpInside)
        //  设置字体大小
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
        //  设置文本及文本颜色
        button.setTitle("登录", forState: .Normal)
        
        //设置颜色
        button.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        
        
        return button

        
        
    }()
    
    
    private lazy var messageLabel: UILabel = {
        
        let messageLabel = UILabel()
        
        messageLabel.font = UIFont.systemFontOfSize(14)
        
        messageLabel.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        
       
        messageLabel.textColor = UIColor.darkGrayColor()
        
        //多行
        messageLabel.numberOfLines = 0
        
        //居中
        messageLabel.textAlignment = .Center
        
        return messageLabel
        
    }()
    
    
    
    
    
    
    
    override init(frame:CGRect) {
        
        super.init(frame: frame)
        
        backgroundColor = UIColor(white: 237 / 255, alpha: 1)
        
        
    }
    
    
    
    

    required init?(coder aDecoder: NSCoder) {
        
        super.init(coder: aDecoder)
        
        backgroundColor = UIColor.yellowColor()
        
        
        
        
    }
    
    
    
    override func layoutSubviews() {
        
        super.layoutSubviews()
        
        //添加子控件
        addSubview(cycleImageView)
        addSubview(maskIconImageView)
        addSubview(iconImageView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        //布局
        cycleImageView.snp_makeConstraints { (make) -> Void in
            //居中
            make.center.equalTo(self)
            
            
        }
        
        maskIconImageView.snp_makeConstraints { (make) -> Void in
            
            make.center.equalTo(cycleImageView)
            
            
        }
        
        
        iconImageView.snp_makeConstraints { (make) -> Void in
            
            make.center.equalTo(cycleImageView)
            
        }
        
        
        messageLabel.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(cycleImageView.snp_bottom)
            make.centerX.equalTo(cycleImageView)
            make.width.equalTo(224)
            
        }
        
        registerButton.snp_makeConstraints { (make) -> Void in
            
        make.leading.equalTo(messageLabel)
            
        make.top.equalTo(messageLabel.snp_bottom).offset(10)
            
        make.size.equalTo(CGSize(width: 100, height: 35))
            
            
        }
        
        loginButton.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(registerButton)
            make.trailing.equalTo(messageLabel)
            make.size.equalTo(registerButton)
            
        }
        
    }
    
    
    
    //MARK: - 开启动画
    private func startAnimation() {
        
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        
        //动画目的地
        animation.toValue = 2 * M_PI
        
        //时长
        animation.duration = 20
        
        // 动画重复次数
        animation.repeatCount = MAXFLOAT
        
        // 失去焦点不释放资源 也即是修复了点击tabbar动画仍然连续播放
        animation.removedOnCompletion = false

        // 执行动画
        cycleImageView.layer.addAnimation(animation, forKey: nil)
        
        
    }
    
    
    //公共方法  外界可以调用public 可以省略
     func upDataVisitorImageAndLabel(message: String?, imageName: String?) {
        
        //如果输入的文字科图片不是nil
        if let msg = message, imgName = imageName {
            
            messageLabel.text = msg
            
            iconImageView.image = UIImage(named: imgName)
            
            //隐藏旋转动画图片
            cycleImageView.hidden = true
            
        }else {//如果输入的文字科图片是nil 那么我们就不设置图片和内容 让他显示父类的  即首页的
            
            
            
            //显示动画
            cycleImageView.hidden = false
            
            //开启动画
            startAnimation()
            
            
            
        }
        
        
        
    }
    
    
    
    // MARK: --点击事件
    @objc private func registerAction() {
        
        print("注册点击")
        
        registerLoginClosure?()
        
        
    }
    
    
    
    @objc private func loginAction() {
        
        print("登录点击")
        
        registerLoginClosure?()
        
    }
    

    


}
